<template>
  <div class="app-container">
    <el-form
      class="searchform"
      @submit.native.prevent
      :model="queryParams"
      ref="queryParams"
      size="small"
      label-width="100px"
    >
      <el-row>
        <el-col :span="5">
          <el-form-item label="剂量计编号">
            <el-input
              v-model="queryParams.dosageNumber"
              placeholder="请输入"
              clearable
              @keyup.enter.native="getList()"
            />
          </el-form-item>
        </el-col>
        <el-col :span="9">
          <el-form-item label-width="20px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="getList()"
              >搜索</el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="reset()"
              >重置</el-button
            >
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>

    <div class="tableoperation" style="display: flex; justify-content: right">
      <el-button
        type="primary"
        icon="el-icon-download"
        size="mini"
        plain
        @click.native.prevent="handleExport()"
        style="margin-right: 8px"
        >导出</el-button
      >
      <print
        :disabled="dataList.length == 0"
        :title="title"
        :beforeMethod="beforeMethods"
      >
        <table
          id="table"
          style="width: 100%"
          border="1"
          cellspacing="0"
          cellpadding="0"
          align="center"
        >
          <tr>
            <td
              colspan="8"
              style="line-height: 30px; text-align: left; padding-left: 10px"
            >
              仪器名称： ，仪器型号： ，仪器编号： ，刻度因子： ，环境温度：
            </td>
          </tr>
          <tr>
            <td
              colspan="8"
              style="line-height: 30px; text-align: left; padding-left: 10px"
            >
              <span style="margin-right: 100px">检测人：</span>
              <span>复核人：</span>
            </td>
          </tr>
        </table>
        <table
          id="table"
          style="width: 100%; margin-top: -2px"
          border="1"
          cellspacing="0"
          cellpadding="0"
          align="center"
        >
          <thead>
            <tr>
              <td>个人剂量计编号</td>
              <td>实验室检测日期</td>
              <td>样品受理编号</td>
              <td>委托单位名称</td>
              <td>射线及防护类型</td>
              <td>Hp(10)(未穿铅衣)<br />(mSv)</td>
              <td>铅衣内Hp(10)<br />(mSv)</td>
              <td>铅衣外Hp(10)<br />(mSv)</td>
            </tr>
          </thead>
          <tr v-for="(item, index) in printList" :key="index">
            <td>{{ item.dosageNumber }}</td>
            <td>{{ parseTime(item.detectionTime, "{y}-{m}-{d}") }}</td>
            <td></td>
            <td>{{ item.entrustOrganizationName }}</td>
            <td>
              <span v-if="item.dosageType == 0">未穿铅衣</span>
              <span v-if="item.dosageType == 1">铅衣内</span>
              <span v-if="item.dosageType == 2">铅衣外</span>
              <span v-if="item.dosageType == 3">本底</span>
            </td>
            <td>{{ item.normalValue }}</td>
            <td>{{ item.withinValue }}</td>
            <td>{{ item.outsideValue }}</td>
          </tr>
          <tr>
            <td v-for="(sumItem, sumIndex) in sums" :key="sumIndex">
              {{ sumItem }}
            </td>
          </tr>
        </table>
      </print>
    </div>
    <div
      v-if="title"
      style="
        margin: -25px 0 15px;
        font-size: 20px;
        font-weight: bold;
        width: 100%;
        text-align: center;
      "
    >
      {{ title }}
    </div>
    <el-table
      v-loading="loading"
      :max-height="height - 400 + 'px'"
      :data="dataList"
      show-summary
      :summary-method="summaryMethod"
    >
      <el-table-column
        label="姓名"
        align="center"
        prop="personnelName"
        width="150"
      />
      <el-table-column
        label="剂量计编号"
        align="center"
        prop="dosageNumber"
        width="150"
      />
      <el-table-column
        label="委托单位名称"
        align="center"
        prop="entrustOrganizationName"
      />
      <el-table-column
        label="实验室检测日期"
        align="center"
        prop="detectionTime"
      >
        <template slot-scope="{ row }">
          {{ parseTime(row.detectionTime, "{y}-{m}-{d}") }}
        </template>
      </el-table-column>
      <el-table-column label="铅衣类型" align="center" prop="dosageType">
        <template slot-scope="{ row }">
          <span v-if="row.dosageType == 0">未穿铅衣</span>
          <span v-if="row.dosageType == 1">铅衣内</span>
          <span v-if="row.dosageType == 2">铅衣外</span>
          <span v-if="row.dosageType == 3">本底</span>
        </template>
      </el-table-column>
      <el-table-column
        label="未穿铅衣(mSv)"
        align="center"
        prop="normalValue"
      />
      <el-table-column label="铅衣内(mSv)" align="center" prop="withinValue" />
      <el-table-column label="铅衣外(mSv)" align="center" prop="outsideValue" />
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { personalResultRecord, personalResultMonitor } from "@/api/form/form";
import { entrustOrganizationListing } from "@/api/person/entrustOrganization";
import {
  entrustWorkingPersonnelList,
  entrustWorkingPersonnelExport,
  entrustWorkingPersonnelDetail,
  downloadTemplate,
  entrustWorkingPersonnelAdd,
  importData,
  entrustWorkingPersonnelEdit,
  entrustWorkingPersonnelDelete,
  listByDosageNumber,
} from "@/api/person/person";
import { listOss, listByIds, delOss } from "@/api/system/oss";
import { getToken } from "@/utils/auth";

export default {
  name: "User",
  dicts: ["seal_shape", "seal_type"],
  data() {
    return {
      // 表单参数
      form: {},
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 视图高度
      height: document.documentElement.clientHeight,
      imageUrlT: "",
      // 列表数据
      dataList: [],
      total: 0,
      dialogVisible: false,
      status: "",
      title: "个人剂量监测记录",
      printList: [],
      sums: [],
    };
  },
  created() {
    this.getList();
  },
  methods: {
    summaryMethod(param) {
      const { columns, data } = param;
      const sums = [];
      // 以下是对小计和总计分别进行赋值
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = "总计";
          return;
        }
        //将当前列的所有值存储到values中  注：非数字会返回NaN。
        const values = data.map((item) => Number(item[column.property]));
        let label = [
          "dosageNumber",
          "personnelName",
          "entrustOrganizationName",
          "detectionTime",
          "dosageType",
        ];
        //如果label包含当前column.property，则进行数据处理
        if (!label.includes(column.property)) {
          // 总计
          let allTotal = values.reduce((prev, curr) => {
            const value = Number(curr);
            // 如果是数字则进行累加
            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);
          allTotal =
            Number.isFinite(allTotal) && !Number.isInteger(allTotal)
              ? allTotal.toFixed(2)
              : allTotal;
          sums[index] = allTotal;
        } else {
          sums[index] = "";
        }
      });
      this.sums = sums;
      return sums;
    },
    async beforeMethods() {
      let queryParams = JSON.parse(JSON.stringify(this.queryParams));
      queryParams.pageSize = this.total;
      queryParams.pageNum = 1;
      let response = await personalResultMonitor(queryParams);
      this.printList = response.rows;
    },
    // 导出
    handleExport() {
      this.download(
        "/statistics/personalResultMonitor/export",
        {
          ...this.queryParams,
        },
        `个人剂量监测记录.xlsx`
      );
    },
    // 获取列表
    getList() {
      this.loading = true;
      personalResultMonitor(this.queryParams)
        .then((response) => {
          this.dataList = response.rows;
          this.total = response.total;
          this.loading = false;
        })
        .catch((response) => {
          this.loading = false;
        });
    },
    // 查询重置
    reset() {
      (this.queryParams = {
        pageNum: 1,
        pageSize: 10,
      }),
        this.resetForm("queryParams");
      this.getList();
    },
  },
};
</script>

<style lang="scss" scoped>
::v-deep .addDialogr {
  .el-form {
    margin-left: -30px;
  }
}
.el-table {
  overflow: visible !important;
}
#table {
  td {
    text-align: center;
  }
}
</style>
